<template>
  <el-scrollbar height="400px">
    <el-row>
      <el-col :span="4">
        <div class="grid-content bg-purple">
          序号
        </div>
      </el-col>
      <el-col :span="4">
        <div class="grid-content bg-purple-light">
          项目ID
        </div>
      </el-col>
      <el-col :span="4">
        <div class="grid-content bg-purple">
          项目名称
        </div>
      </el-col>
      <el-col :span="8">
        <div class="grid-content bg-purple-light">
          状态
        </div>
      </el-col>
      <el-col :span="4">
        <div class="grid-content bg-purple">
          管理
        </div>
      </el-col>
    </el-row>

    <el-row v-for="(value,index) in res2 " :key="index">
      <el-col :span="4">
        <div class="grid-content bg-purple">
          {{ index }}
        </div>
      </el-col>
      <el-col :span="4">
        <div class="grid-content  bg-purple-light">
          {{ value.id }}
        </div>
      </el-col>
      <el-col :span="4">
        <div class="grid-content bg-purple">
          {{ value.projectName }}
        </div>
      </el-col>
      <el-col :span="8">
        <div class="grid-content  bg-purple-light">
          <el-progress :percentage="percentage[index]" :format="format"/>

        </div>
      </el-col>
      <el-col :span="4">
        <div class="grid-content bg-purple">
          <el-button @click="mange(value.id,index)">管理</el-button>
        </div>
      </el-col>
    </el-row>

  </el-scrollbar>
</template>

<script setup lang="ts">

import getProjectByUserId from "@/api/project/getProjectByUserId";
import {ref, watch} from "vue";
import {ElMessageBox, ElMessage} from 'element-plus'
import type {Action} from 'element-plus'
import setProjectStatus from "@/api/project/setProjectStatus";
import getProjectPercentage from "@/api/project/getProjectPercentage";
import setProjectOver from "@/api/project/setProjectOver";
// import getProjectPercentage from "@/api/project/getProjectPercentage";

let status: number = parseInt(sessionStorage.getItem(`project.status`));
let res2 = ref<[]>([]);
const percentage = ref<[]>([]);

watch(() => status, async (newValue) => {

  if (isNaN(newValue)) {
    status = null;
  }
  let res = getProjectByUserId(status);
  await res.then((data) => {
    for (let i = 0; i < data.length; i++) {
      res2.value?.push(data[i]);
    }
  })
  if (status != null) {
    for (let i = 0; i < res2.value.length; i++) {
      percentFun(res2.value[i].id)
    }
  }else {
    for (let i = 0; i < res2.value.length; i++) {
      percentage.value[i] = 0;
    }
  }

}, {
  immediate: true,
  deep: true
})


const format = (percentage: number) => (percentage === 100 ? 'success' : `${percentage}%`)

const mange = (id: number,index: number) => {
  if (status == null) {
    ElMessageBox.confirm('该项目审核通过', '审核', {
      confirmButtonText: 'OK',
      callback: (action: Action) => {

        if (action == "confirm") {
          setProjectStatus(id, status);
        }
        ElMessage({
          type: 'info',
          message: `action: ${action}`,
        })
      },
    })
  } else if (status == 0) {
    if (percentage.value[index] == 100){
      ElMessageBox.confirm('交付该项目', '交付', {
        confirmButtonText: 'OK',
        callback: (action: Action) => {

          if (action == "confirm") {
            setProjectOver(id);
          }
          ElMessage({
            type: 'info',
            message: `action: ${action}`,
          })
        },
      })
    }else {
      alert("该项目尚未完成")
    }
  }else if (status == 1){
    alert("无效操作")
  }
}


const percentFun = async (id: number) => {

  await getProjectPercentage(id)
      .then((data) => {
        percentage.value.push(data * 100);
      })

}
</script>

<style scoped>

.el-row {
  margin-bottom: 20px;
}

.el-row:last-child {
  margin-bottom: 0;
}

.el-col {
  border-radius: 4px;
}

.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}

.scrollbar-demo-item {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 50px;
  margin: 10px;
  text-align: center;
  border-radius: 4px;
  background: var(--el-color-primary-light-9);
  color: var(--el-color-primary);
}
</style>